package com.tj.template.application.vo;

import com.jsoniter.JsonIterator;
import com.tj.bo.common.conf.vo.MarketTimePrice;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: binary (mr.vencnet@gmail.com)
 * Date: 2020-06-29
 * Time: 23:55
 */
public class Bucket {

    private Long marketTimestamp;

    private Object object;

    private long startTime;
    private long endTime;

    private double maxPrice = 0;

    private double minPrice = 0;

    private double totalPrice = 0;

    private double avgPrice = 0;

    private int cnt = 0;

    private MarketTimePrice marketTimePrice;

    private Bucket lastBucket;

//    private List<ZSetOperations.TypedTuple<Object>> marketTimePrices;

    public Bucket() {
    }

    public Bucket(Long marketTimestamp, Object object) {
        this.marketTimestamp = marketTimestamp;
        this.object = object;
    }

    public Bucket(long startTime, long endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
        this.cnt = 0;
//        this.marketTimePrices = new ArrayList<>();
    }

    public void add(ZSetOperations.TypedTuple<Object> marketTimePrice) {
        MarketTimePrice value = JsonIterator.deserialize((String) marketTimePrice.getValue(), MarketTimePrice.class);
        Double marketPrice = Double.valueOf(value.getMarketPrice());

        cnt++;
        totalPrice += marketPrice;
        avgPrice = totalPrice / cnt;

        if (maxPrice == -1) {
            maxPrice = marketPrice;
            minPrice = marketPrice;
        } else {
            if (marketPrice > maxPrice) {
                maxPrice = marketPrice;
            } else if (marketPrice < minPrice) {
                minPrice = marketPrice;
            }
        }

        if (this.marketTimePrice == null) {
            this.marketTimePrice = new MarketTimePrice()
                    .setBusinessTime(value.getBusinessTime())
                    .setType(value.getType())
                    .setMarketTimestamp(value.getMarketTimestamp())
                    .setMarketPrice(value.getMarketPrice())
            ;
        }

    }

    public MarketTimePrice getMarketTimePrice() {
        return marketTimePrice;
    }

    public Bucket setMarketTimePrice(MarketTimePrice marketTimePrice) {
        this.marketTimePrice = marketTimePrice;
        return this;
    }

    public Bucket getLastBucket() {
        return lastBucket;
    }

    public Bucket setLastBucket(Bucket lastBucket) {
        this.lastBucket = lastBucket;
        return this;
    }

    public long getStartTime() {
        return startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public double getMaxPrice() {
        return maxPrice;
    }

    public double getMinPrice() {
        return minPrice;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public double getAvgPrice() {
        return avgPrice;
    }

    public int getCnt() {
        return cnt;
    }

    public Long getMarketTimestamp() {
        return marketTimestamp;
    }

    public Bucket setMarketTimestamp(Long marketTimestamp) {
        this.marketTimestamp = marketTimestamp;
        return this;
    }

    public Object getObject() {
        return object;
    }

    public Bucket setObject(Object object) {
        this.object = object;
        return this;
    }

    public Bucket setStartTime(long startTime) {
        this.startTime = startTime;
        return this;
    }

    public Bucket setEndTime(long endTime) {
        this.endTime = endTime;
        return this;
    }

    public Bucket setMaxPrice(double maxPrice) {
        this.maxPrice = maxPrice;
        return this;
    }

    public Bucket setMinPrice(double minPrice) {
        this.minPrice = minPrice;
        return this;
    }

    public Bucket setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
        return this;
    }

    public Bucket setAvgPrice(double avgPrice) {
        this.avgPrice = avgPrice;
        return this;
    }

    public Bucket setCnt(int cnt) {
        this.cnt = cnt;
        return this;
    }

    //    public List<ZSetOperations.TypedTuple<Object>> getMarketTimePrices() {
//        return marketTimePrices;
//    }
}
